package com.gzhryc.shared_device.oem.h5.mng;

import com.gzhryc.common.config.PropertyTools;
import com.gzhryc.common.logging.Logger;
import com.gzhryc.log.dao.db.ManagerVisitRecord;
import com.gzhryc.log.services.ManagerVisitRecordService;
import com.gzhryc.servlet.WebUtils;
import com.gzhryc.system.dao.db.Manager;
import eu.bitwalker.useragentutils.Browser;
import eu.bitwalker.useragentutils.UserAgent;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

public class WebConstants {

	static Logger log = Logger.getLogger(WebConstants.class);
	
	public final static String PROGRAM_TYPE = "h5_mng";
	public final static String PROGRAM_TYPE_NAME = "后台管理";
	
	public final static String CONF_RES_DOMAIN_KEY = "resDomain";
	public final static String CONF_LOCAL_RES_KEY = "localRes";

	public final static Integer PAGE_LIMIT = 20;

	public final static String SESSION_BXSCN_BT_DM_CONTENT = "_bxscn_bt_dm_content";
	public final static String SESSION_PCL_SET_MEAL_CONTENT = "_pcl_set_meal_content";
	public final static String SESSION_ORDER_EXPORT_EXECL_SEARCH = "_order_export_execl_search";
	public final static String SESSION_BELONG_DEVICE_SN_LIST_STR = "_belong_device_sn_list_str";
	
	private static int debug = -1;
	public static int isDebug() {
		if(debug == -1) {
			debug = PropertyTools.get().getInt(PROGRAM_TYPE + ".isDebug", 0);
		}
		return debug;
	}
	
	public static boolean isLocalRes() {
		return PropertyTools.get().getBoolean(CONF_LOCAL_RES_KEY,false);
	}
	
	/**
	 * 获取资源域名
	 * @param request
	 * @return
	 */
	public static String getResDomain(HttpServletRequest request) {
		//检查是否使用本地资源
		boolean localRes = PropertyTools.get().getBoolean(CONF_LOCAL_RES_KEY,false);
        if(localRes) {
        	return request.getContextPath();
        }else {
        	return PropertyTools.get().getString(CONF_RES_DOMAIN_KEY);
        }
	}

	/**
	 * 记录日志
	 *
	 * @param httpRequest
	 * @param manager
	 */
	public static void setSysLog(HttpServletRequest httpRequest, Manager manager) {
		try {
			// 封装信息
			ManagerVisitRecord managerVisitRecord = new ManagerVisitRecord();
			managerVisitRecord.setManagerId(manager.getId()); // 用户id
			managerVisitRecord.setManagerName(manager.getName()); // 用户名
			managerVisitRecord.setPath(httpRequest.getRequestURI()); // 访问路径
			managerVisitRecord.setIp(WebUtils.getClientIP(httpRequest)); // 访问的IP地址
			managerVisitRecord.setCreateDate(new Date()); // 访问时间
			managerVisitRecord.setMethod(httpRequest.getMethod()); // 请求方式
			managerVisitRecord.setLang(WebUtils.getLanguage(httpRequest)); // 语言
			managerVisitRecord.setScheme(httpRequest.getScheme()); // 域名
			managerVisitRecord.setQueryString(httpRequest.getQueryString()); // 查询字符串
			managerVisitRecord.setAccept(httpRequest.getHeader("Accpet")); // 访问内容类型
			managerVisitRecord.setUserAgent(httpRequest.getHeader("User-Agent")); // 访问代理信息
			managerVisitRecord.setNote(httpRequest.getHeader("note")); // 访问描述
			managerVisitRecord.setFormJson(httpRequest.getHeader("Form_Json")); // form表单中数据
			managerVisitRecord.setIsXmlHttpRequest(WebUtils.isXMLContentType(httpRequest)); // 是否是XmlHttp访问
			String user_agent = httpRequest.getHeader("User-Agent");

			// 设置客户端类型
			if (user_agent.contains("Android")) {
				managerVisitRecord.setClientType(4);
			} else if (user_agent.contains("iPhone")) {
				managerVisitRecord.setClientType(3);
			} else if (user_agent.contains("iPad")) {
				managerVisitRecord.setClientType(2);
			} else if (user_agent.contains("Windows")) {
				managerVisitRecord.setClientType(1);
			} else {
				managerVisitRecord.setClientType(0);
			}

			Browser browser = UserAgent.parseUserAgentString(httpRequest.getHeader("User-Agent")).getBrowser(); // 获取浏览器对象
			managerVisitRecord.setVisitSoftwareName(browser.getName()); // 浏览器名称

			// 设置contextPath
			String contextPath = httpRequest.getContextPath();
			if (StringUtils.isNotEmpty(contextPath)) {
				managerVisitRecord.setContextPath(contextPath);
			}
			managerVisitRecord.setContextPath("/");

			// 设置referer
			String referer = httpRequest.getHeader("referer");
			if (referer != null) {
				managerVisitRecord.setReferer(referer);
			}

			ManagerVisitRecordService.self().add(managerVisitRecord);
		} catch (Exception e) {
			log.error(e.getMessage(), e);
		}
	}
}
